package com.ruoyi.order.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;
import com.ruoyi.order.utils.DivisionUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.annotation.Resource;

@Component
public class KafkaConfig {
    @Resource
    StringRedisTemplate redis;
    @Resource
    ITbOrderService tbOrderService;
    @Resource
    DivisionUtil divisionUtil;
    @KafkaListener(topics = "TableThree")
    public void KafkaConfigListener(String msg){
        System.out.println(msg);
        String tbOrderRedis = redis.opsForValue().get(msg);
        redis.delete(msg);
        if( tbOrderRedis != null ){
            //订单超时 删除redis缓存数据 更改状态 存入数据库
            redis.delete(msg);
            ObjectMapper objectMapper = new ObjectMapper();
            TbOrder tbOrder = null;
            try {
                tbOrder = objectMapper.readValue(tbOrderRedis, TbOrder.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            tbOrder.setOrderStatus((long)7);
            divisionUtil.DivisionTOThreeTable(tbOrder.toString());
        }
    }
}
